package com.example.entity;

/**
 * @Author: wanqian
 * @Date: 2021/1/26 11:27
 */
public class DoubleLinkedList {

    private DoubleLinkedNode head;

    public DoubleLinkedNode getHead() {
        return head;
    }

    public void setHead(DoubleLinkedNode head) {
        this.head = head;
    }

    public DoubleLinkedList() {
        this.head = new DoubleLinkedNode(0, "");
    }

    public void addNode(DoubleLinkedNode linkedNode) {
        DoubleLinkedNode temp = head;

        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        linkedNode.setPre(temp);
        temp.setNext(linkedNode);
    }

    public void addByOrder(DoubleLinkedNode linkedNode) {
        DoubleLinkedNode temp = head;
        if (temp.getNext() == null) {
            System.out.println("双向链表为空");
        }

        boolean flag = false;
        while (temp.getNext() != null) {
            if (temp.getNext().getId() > linkedNode.getId()) {
                linkedNode.setNext(temp.getNext());
                linkedNode.setPre(temp);
                temp.getNext().setPre(linkedNode);
                temp.setNext(linkedNode);
                flag = true;
                break;
            }
            temp = temp.getNext();
        }
        if (!flag) {
            linkedNode.setPre(temp);
            temp.setNext(linkedNode);
        }
    }

    public void update(DoubleLinkedNode linkedNode) {
        DoubleLinkedNode temp = head.getNext();
        if (temp == null) {
            System.out.println("双向链表为空");
        }

        while (temp != null) {
            if (temp.getId() == linkedNode.getId()) {
                linkedNode.setNext(temp.getNext());
                linkedNode.setPre(temp.getPre());
                if (temp.getNext() != null) {
                    temp.getNext().setPre(linkedNode);
                }
                temp.getPre().setNext(linkedNode);
                return;
            }
            temp = temp.getNext();
        }
    }

    public void delete(int id) {
        DoubleLinkedNode temp = head.getNext();
        if (temp == null) {
            System.out.println("双向链表为空");
        }

        while (temp != null) {
            if (temp.getId() == id) {
                temp.getPre().setNext(temp.getNext());
                temp.getNext().setPre(temp.getPre());
                return;
            }
            temp = temp.getNext();
        }
    }

    public void list() {
        DoubleLinkedNode temp = head.getNext();
        if (temp == null) {
            System.out.println("双向链表为空");
        }

        while (temp != null) {
            System.out.println(temp);
            temp = temp.getNext();
        }
    }

    public int length() {
        DoubleLinkedNode temp = head.getNext();
        if (temp == null) {
            System.out.println("双向链表为空");
        }

        int num = 0;
        while (temp != null) {
            num++;
            temp = temp.getNext();
        }
        System.out.println("双向链表长度： " + num);
        return num;
    }
}
